% Parameters for orientation channel 

%% input parameters
fpath = '../../img/balloons.png'; %'pout.tif';
color_img = 1;  % flag to enable pre-processing if color-image

nr = 256;       %input image size - num of rows
nc = 256;        %input image size - num of cols

q = quantizer('fixed', [24,20]);    %input data representation
input_file_generate = 0; % if generating input dat file is needed
datfile = 'image_256_norm_frac20.dat'; %'image_RminusG_256.dat';

%% algorithmic parameters
dec_factor = 2; %decimate factor for each pyramid level
window = 5;     %filter window size in pyramid gen and resampling steps

Nc = 1;         %number of center channels
center_ch = [1];    %the actual center levels
Ns = 2;         %num of surrounds for each center ch
del = [3, 4];   %delta for surround channel w.r.t each center ch
output_scale = 4;   % the scale of the output saliency/conspicuity map
delta = 6;      % number of levels of pyramid 

N_theta = 1;
theta = [pi/4, pi/2, 3*pi/4, 0];

outnorm = 0;    % flag to enable normalization after across-scale addition

if(window == 5)
    filt_coeff = (1/16)*[1, 4, 6, 4, 1];    %filter coefficients n each dimension
elseif(window == 9)
    filt_coeff = (1/64)*[1, 4, 8, 12, 14, 12, 8, 4, 1];
end

filt_coeff_2d = filt_coeff'*filt_coeff;     %filter coeffs for 2D convolution


%% for comparing hardware result
result_file = '../../saliency_ML605/saliency_ML605/saliency_result.dat';  %'../saliency_ml605/orientation_channel_result.dat'; 
error_threshold = 1e-4;
result_flag = 1;

qout = quantizer('fixed', [24,20]);  %output data rep
%% pre-processing
if(color_img == 1)
    im1 = imread(fpath);
    im2 = imresize(im1, [nr, nc]);
    img = double(im2)./255 ;
    [I R G B Y] = SalColorMap(img);    

    img = I; %abs(R - G);   
    if(input_file_generate == 1)        
        img_q = quantize(q, img);
        %convert to column vector (row-major)
        img_1d = reshape(img_q', nr*nc, 1);
        % convert to hex
        img_1d_hex = num2hex(q, img_1d);

        % Write into the file
        fid = fopen(datfile, 'w');
        for i=1:size(img_1d_hex,1)
            fprintf(fid,'%6s\n', img_1d_hex(i,:));
        end
        fclose(fid);
    else
        img = quantize(q, img);
    end
else
    if(input_file_generate == 1)
        img = img2dat(fpath, [nr,nc], datfile, q);
    else
        I = imread(fpath);
        % Resize image to required matrix size
        img1 = imresize(double(I), [nr,nc]);    
        img = quantize(q, img1);
    end
end

%% Run the orientation channel

pars.img_size_x = nr;
pars.img_size_y = nc;
pars.levels = delta;
pars.Nc = Nc;
pars.Ns = Ns;
pars.c = center_ch; 
pars.delta = del;
pars.pyr_filt_coeff = filt_coeff_2d;
pars.resample_filt_coeff = filt_coeff_2d;
pars.outscale = output_scale;
pars.dec_factor = dec_factor;
pars.theta = theta;
pars.N_theta = N_theta;

output_nr = nr/(dec_factor^output_scale);
output_nc = nc/(dec_factor^output_scale);
% the intensity channel function
orientation_ch_sim_acc = zeros(output_nr, output_nc);

intensity_ch_sim_norm = intensity_channel_sim(img, pars);
for i = 1:4
    orientation_ch_sim = orientation_channel_sim(img, pars);    
    orientation_ch_sim_acc = orientation_ch_sim_acc + orientation_ch_sim;
end
orientation_ch_sim_norm = maxnorm_sim(orientation_ch_sim_acc);

orientation_ch_sim_norm = orientation_ch_sim_norm + intensity_ch_sim_norm;

if(result_flag)
        array_out = dat2array(result_file, output_nr*output_nc, qout);
        result_hw = reshape(array_out, output_nc, output_nr)';
        abs_error = abs(orientation_ch_sim_norm - result_hw);

        
        max_error = max(max(abs_error))
        avg_error = mean(mean(abs_error))

        error_flag = abs_error > error_threshold;
        num_error = sum(sum(error_flag))
        error_flag = error_flag * 1111;
end